package com.kyh.util.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DbUtil {
	private static String jdbcurl = "jdbc:mysql://rm-wz9vznbs4523j54v2jo.mysql.rds.aliyuncs.com:3306/yjr_test";
	private static String jdbcusername = "bo";
	private static String jdbcpassword = "ageAEHadgF4HGSg3adagaew7%^EB6aA";
	

	private static Connection connect;
	private static DbUtil instance;
	private DbUtil(){}
	public static DbUtil getInstance() {
		if (instance == null) {
			instance = new DbUtil();
			try {
				Class.forName("com.mysql.jdbc.Driver"); // 加载MYSQL JDBC驱动程序
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return instance;
	}
	
	public String selectCol(String tablename, String colname, String wherecolname, String wherecolvalue) {
		try {
			if (connect == null) {
				connect = DriverManager.getConnection(jdbcurl, jdbcusername, jdbcpassword);
			}

			String sqlStr = "select * from " + tablename;
			if (wherecolname != null && !"".equals(wherecolname)) {
				sqlStr = sqlStr + " where " + wherecolname + " = " + wherecolvalue;
			}
			System.out.println(sqlStr);
			Statement stmt = connect.createStatement();
			ResultSet rs = stmt.executeQuery(sqlStr);
			while (rs.next()) {
				System.out.println("colvalue=====" + rs.getString(colname));
				return rs.getString(colname);
			}
			rs.close();
			connect.close();
			return "";
		} catch (SQLException e) {
			e.printStackTrace();
			return "";
		}
	}
	
	public static void main(String args[]) {
		try {
			Connection connect = DriverManager.getConnection(jdbcurl, jdbcusername, jdbcpassword);

			Statement stmt = connect.createStatement();
			ResultSet rs = stmt.executeQuery("select * from arcticle");
			while (rs.next()) {
				System.out.println("arcticletitle=====" + rs.getString("arcticletitle"));
			}
			rs.close();
			connect.close();
		} catch (Exception e) {
			System.out.print("get data error!");
			e.printStackTrace();
		}
	}
}
